<?php
/**
 * SortUtility
 *
 * Ex. SortUtility::arrayNatsortList($tarArr, false, "key");
 * @package    WacStorehouse
 * @subpackage lib/utils
 * @author     ben
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */

class SortUtility{
	public static function arrayNatsortList($array,$isReverse=false) {
	   // for all arguments without the first starting at end of list
	   for ($i=func_num_args();$i>2;$i--) {
	       // get column to sort by
	       $sort_by = func_get_arg($i-1);
	       // clear arrays
	       $new_array = array();
	       $temporary_array = array();
	       // walk through original array
	       foreach($array as $original_key => $original_value) {
	           // and save only values
	           $temporary_array[] = $original_value[$sort_by];
	       }
	       // sort array on values
	       natsort($temporary_array);
	       // delete double values
	       $temporary_array = array_unique($temporary_array);
	       
	       if($isReverse){
	       	$temporary_array = array_reverse($temporary_array);
	       }
	       
	       // walk through temporary array
	       foreach($temporary_array as $temporary_value) {
	           // walk through original array
	           foreach($array as $original_key => $original_value) {
	               // and search for entries having the right value
	               if($temporary_value == $original_value[$sort_by]) {
	                   // save in new array
	                   $new_array[$original_key] = $original_value;
	               }
	           }
	       }
	       // update original array
	       $array = $new_array;
	   }
	   return $array;
	}
}


?>